/**   
* @ProjectName: ShopSytem
* @Title: AdminDaoImpl.java 
* @Package com.element5th.shop.dao.admin 
* @Description: 对数据库进行管理员相关的操作
* @author 第五元素小组   
* @date 2012-10-26 10:08:31 
* @version V1.0   
*/ 
package com.element5th.shop.dao.admin;

import java.util.ArrayList;
import java.util.List;

import com.element5th.shop.comm.BaseDao;
import com.element5th.shop.comm.BusinessException;
import com.element5th.shop.comm.SystemException;
import com.element5th.shop.dto.BaseDto;
import com.element5th.shop.entity.Admin;
import com.element5th.shop.entity.AdminLimit;


/**
 * The Class AdminDaoImpl.
 */
public class AdminDaoImpl extends BaseDao implements IAdminDao {

	
	/*  
	* Title: adminLogin 
	* Description: 进行管理员登录验证
	* @param dto 登录的用户名及密码
	* @return 登录管理员信息，登录失败抛出异常
	* @see com.element5th.shop.dao.admin.IAdminDao#adminLogin(com.element5th.shop.dto.BaseDto) 
	*/ 
	@Override
	public BaseDto adminLogin(BaseDto dto) 
			throws SystemException, BusinessException{
		this.openConnection();
		Admin admin = this.findByCol(Admin.class, "adminName", dto.getAdmin().getAdminName());
		if(admin == null) {
			this.rollback();
			throw new BusinessException(3001, "用户名不存在");
		} else if (admin.getAdminPwd().equals(dto.getAdmin().getAdminPwd()) == false) {
			this.rollback();
			throw new BusinessException(3002, "密码错误");
		} else {
			dto.setAdmin(admin);
			dto.setAdminLimit(this.findById(AdminLimit.class, admin.getLimitId()));
			this.commit();
			return dto;
		}
	}

	
	/*  
	* Title: addAdmin 
	* Description: 添加管理员
	* @param dto 需要添加的管理员的信息
	* @return 操作是否成功
	* @see com.element5th.shop.dao.admin.IAdminDao#addAdmin(com.element5th.shop.dto.BaseDto) 
	*/ 
	@Override
	public boolean addAdmin(BaseDto dto) {
		this.openConnection();
		boolean result = this.save(dto.getAdmin());
		this.commit();
		return result;
	}

	
	/*  
	* Title: addLimit 
	* Description: 为管理员添加权限
	* @param dto 要添加权限的管理员
	* @return  操作是否成功
	* @see com.element5th.shop.dao.admin.IAdminDao#addLimit(com.element5th.shop.dto.BaseDto) 
	*/ 
	@Override
	public boolean addLimit(BaseDto dto) {
		this.openConnection();
		boolean result = this.save(dto.getAdminLimit());
		this.commit();
		return result;
	}

	
	/*  
	* Title: removeLimit 
	* Description: 取消管理员的权限
	* @param dto 要取消权限的管理员
	* @return 操作是否成功
	* @see com.element5th.shop.dao.admin.IAdminDao#removeLimit(com.element5th.shop.dto.BaseDto) 
	*/ 
	@Override
	public boolean removeLimit(BaseDto dto) {
		this.openConnection();
		dto.getAdmin().setLimitId(null);
		boolean result = this.update(dto.getAdmin(), dto.getAdmin().getAdminId());
		this.commit();
		return result;
	}

	
	/*  
	* Title: adminList 
	* Description: 查看所有管理员列表
	* @return 管理员列表
	* @see com.element5th.shop.dao.admin.IAdminDao#adminList() 
	*/ 
	@Override
	public List<BaseDto> adminList() {
		this.openConnection();
		List<Admin> list = this.findAll(Admin.class);
		List<BaseDto> listDto = new ArrayList<BaseDto>();
		for (Admin admin : list) {
			BaseDto dto = new BaseDto();
			dto.setAdmin(admin);
			listDto.add(dto);
		}
		this.commit();
		return listDto;
	}

}
